package remote_work.client;

import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.custom.SashForm;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Text;

import remote_work.dto.InputObject;
import remote_work.dto.OutputObject;
import remote_work.util.RequestService;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

/**
 * @author LiuPing
 *
 * 2018年3月13日
 */
public class LoginUI {

	protected Shell shell;
	
	private Text text_username;
	private Text text_password;
	
	/**
	 * Launch the application.
	 * @param args
	 */
	public static void main(String[] args) {
		try {
			LoginUI window = new LoginUI();
			window.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		shell.open();
		shell.layout();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 */
	protected void createContents() {
		shell = new Shell();
		shell.setSize(450, 300);
		shell.setText("远程作业处理系统登录");
		shell.setLayout(new FillLayout(SWT.HORIZONTAL));
		
		SashForm sashForm = new SashForm(shell, SWT.NONE);
		
		Composite composite = new Composite(sashForm, SWT.NONE);
		
		Label label_username = new Label(composite, SWT.NONE);
		label_username.setBounds(61, 55, 76, 20);
		label_username.setText("用户名：");
		
		Label label_password = new Label(composite, SWT.NONE);
		label_password.setBounds(61, 113, 76, 20);
		label_password.setText("密    码：");
		
		text_username = new Text(composite, SWT.BORDER);
		text_username.setBounds(143, 52, 201, 26);
		
		text_password = new Text(composite, SWT.BORDER | SWT.PASSWORD);
		text_password.setBounds(143, 110, 201, 26);
		
		Button btn_login = new Button(composite, SWT.NONE);
		btn_login.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				MessageBox mb = new MessageBox(shell);
				// 发送请求到服务器请求登录
				InputObject inputObject = new InputObject();
				inputObject.setMethod("login");
				if(text_username.getText().trim()==""||text_password.getText().trim()==null){
					mb.setMessage("用户名或密码不能为空！");
					mb.open();
					return;
				}
				inputObject.addParams("user", text_username.getText());
				inputObject.addParams("pwd", text_password.getText());
				
				OutputObject outputObject = RequestService.execute(inputObject);
				if ("0".equals(outputObject.getReturnCode())) {
					shell.dispose();
					MainUI window = new MainUI();
					MainUI.setUser(outputObject.getBean());
					window.open();
				} else {
					mb.setMessage("用户名或密码错误!");
					mb.open();
				}
			}
		});
		btn_login.setBounds(79, 178, 98, 30);
		btn_login.setText("登    录");
		
		Button btn_exit = new Button(composite, SWT.NONE);
		btn_exit.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				System.exit(0);
			}
		});
		btn_exit.setBounds(231, 178, 98, 30);
		btn_exit.setText("退    出");
		sashForm.setWeights(new int[] {1});

	}
}
